import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class union_find {
   private int n;
   private int[] parent;

   union_find(int n)
   {
      parent = new int[n];
      for(int i =0;i<n;i++)
      {
         parent[i]=i;
      }
      this.n = n;
   }
   void pair(int a,int b)
   {
      parent[find(b)]=find(a);
   }
   int find(int index)
   {
      if(parent[index]!=index)
      {
         parent[index]=find(parent[index]);
      }
      return parent[index];
   }
   public int groupcount()
   {
      Set<Integer> in = new HashSet<Integer>();
      for(int i =0;i<n;i++)
      {
         in.add(find(i));
      }
      return in.size();
   }
}
class Solution {
    
    public int findCircleNum(int[][] isConnected) {
       int cities = isConnected.length;
       union_find uf = new union_find(cities);
       for(int i =0;i<cities;i++)
       {
         for(int j=0;j<cities;j++)
         {
            if(isConnected[i][j]==1)
            {
                uf.pair(i,j);
            }
         }
       }
       return uf.groupcount();
    }
}